import datetime
import json

from flask import Flask, request, Response
from openai import OpenAI
import requests

app = Flask(__name__)
client = OpenAI()


@app.route('/v1/chat/completions', methods=['OPTIONS', 'POST'])
def v1_chat_completions():
    if request.method == "OPTIONS":
        res = Response()
        res.headers["CF-Cache-Status"] = "DYNAMIC"
        res.headers["CF-RAY"] = "847e0810eab22b85-LAX"
        res.headers["Server"] = "cloudflare"
        headers = {
            'Date': 'Tue, 23 Jan 2024 06:52:43 GMT',
            'Content-Length': '0',
            'Connection': 'keep-alive',
            'access-control-allow-headers': 'authorization,content-type,x-requested-with',
            # 'access-control-allow-methods': 'GET, OPTIONS, POST',
            'access-control-allow-origin': 'http://localhost:3000',
            # 'strict-transport-security': 'max-age=15724800; includeSubDomains',
            # 'CF-Cache-Status': 'DYNAMIC',
            # 'Set-Cookie': '__cf_bm=O1.9QQlAsZxkvHsbUpcgl1nf6LahXLTld2vg320fNfc-1705992763-1-AY8aJCHbpaVwxtMo1Jgv54zeZ7KpYrcTXBFT8r2d2TqsX0RdbhORbDwI/s4valqfqe50nVKAYsHkeqGSkth9byY=; path=/; expires=Tue, 23-Jan-24 07:22:43 GMT; domain=.api.openai.com; HttpOnly; Secure; SameSite=None',
            # 'Set-Cookie': '_cfuvid=NUvPmBtHqS5anhCDp02saa0raAb1XVsOhZw3JGMhjbw-1705992763744-0-604800000; path=/; domain=.api.openai.com; HttpOnly; Secure; SameSite=None',
            # 'Server': 'cloudflare',
            # 'CF-RAY': '849e1d9518d808ec-LAX',
            # 'alt-svc': 'h3=":443"; ma=86400',
        }
        res.headers.update(headers)
        # res.set_cookie("_cfuvid", "NUvPmBtHqS5anhCDp02saa0raAb1XVsOhZw3JGMhjbw-1705992763744-0-604800000", domain="localhost", path="/", expires="Session", secure=True, httponly=True)
        # res.set_cookie("__cf_bm", "O1.9QQlAsZxkvHsbUpcgl1nf6LahXLTld2vg320fNfc-1705992763-1-AY8aJCHbpaVwxtMo1Jgv54zeZ7KpYrcTXBFT8r2d2TqsX0RdbhORbDwI/s4valqfqe50nVKAYsHkeqGSkth9byY=", domain="localhost", path="/", expires=datetime.datetime.now()+datetime.timedelta(1), secure=True, httponly=True)
        # res.headers = {'Date': 'Fri, 19 Jan 2024 09:25:38 GMT', 'Content-Length': '0', 'Connection': 'keep-alive', 'access-control-allow-headers': '', 'access-control-allow-methods': 'GET, OPTIONS, POST', 'strict-transport-security': 'max-age=15724800; includeSubDomains', 'CF-Cache-Status': 'DYNAMIC', 'Set-Cookie': '__cf_bm=WBjvm5nb4EXk_UY0iNEzwYAqskx1wAUD7VboSrASIQM-1705656338-1-ASXlAfjtrsKsSns8IFVHW60EtJFfvb/EyqmGP83uTb644T6orUbNF0PTSRlBmjhEknZujobNxddftFpXlrqI6JI=; path=/; expires=Fri, 19-Jan-24 09:55:38 GMT; domain=.api.openai.com; HttpOnly; Secure; SameSite=None, _cfuvid=EsaIWIDHkkZVFeHeSxLZOb8C4YVWGUrNLQersb5M5ls-1705656338115-0-604800000; path=/; domain=.api.openai.com; HttpOnly; Secure; SameSite=None', 'Server': 'cloudflare', 'CF-RAY': '847e0810eab22b85-LAX', 'alt-svc': 'h3=":443"; ma=86400'}
        return res
    x = request.data
    return Response()
    client.chat.completions.create(messages=[], model="chatgpt-turbo-1106", stream=True)
    c = request.input_stream.read()
    a = 1


@app.route("/", defaults={"path": ""})
@app.route("/<string:path>")
@app.route("/<path:path>")
def catch_all(path):
    obj = request.json
    return 'You want path: %s' % path


app.run("localhost", 40119)
